package com.example.wan1.view.coin;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.example.wan1.R;
import com.example.wan1.adapter.CoinListAdapter;
import com.example.wan1.base.fragment.SimpleDialogFragment;
import com.example.wan1.model.CoinBean;
import com.example.wan1.model.CoinSignInBean;
import com.example.wan1.model.common.CommonResList;
import com.example.wan1.utils.SPUtils;
import com.example.wan1.utils.ToastUtils;
import com.example.wan1.utils.ToolsUtils;
import com.example.wan1.view.login.UserManager;
import com.example.wan1.view.web.JumpTool;
import com.scwang.smart.refresh.footer.BallPulseFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;

/***
 * @Author: zxd
 * @CreateDate: 2020/7/28 16:34
 * @Description: 我的积分获取记录
 */
public class CoinFragment extends SimpleDialogFragment implements CoinContract.View,
        OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.rule)
    TextView mRule;
    @BindView(R.id.coin_text_tv)
    TextView mTvCoinText;
    @BindView(R.id.coin_tv)
    TextView mTvCoin;
    @BindView(R.id.coin_record_tv)
    TextView mTvCoinRecord;
    @BindView(R.id.coin_rv)
    RecyclerView mCoinRv;
    @BindView(R.id.srlheader)
    MaterialHeader mSrlheader;
    @BindView(R.id.srl)
    SmartRefreshLayout mSrl;
    @BindView(R.id.srlfooter)
    BallPulseFooter mFooter;

    @BindView(R.id.coin_tabr)
    Toolbar mToolbar;

    private int page = 1;

    @Inject
    CoinListAdapter mAdapter;
    @Inject
    List<CoinSignInBean> mList;
    @Inject
    CoinPresenter mPresenter;

    @Override
    protected void initData() {
        mPresenter.getCoinSignInData(page);
    }

    @Override
    protected void initView() {
        ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
        ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);

        showLoading();
        //mSrlheader.setColorSchemeResources(R.color.qc_green);
        mSrlheader.setColorSchemeResources(ToolsUtils.getAttrId(getActivity()));
        mRule.setOnClickListener(v -> JumpTool.goWeb(getActivity(), getString(R.string.coin_rule),
                "https://www.wanandroid.com/blog/show/2653"));
        mTitle.setText("我的积分");

        //mList = new ArrayList<>();
        //mAdapter = new CoinListAdapter(R.layout.coin_item, mList);
        mCoinRv.setAdapter(mAdapter);
        mCoinRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        //mCoinRv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        int footerLayoutCount = mAdapter.getFooterLayoutCount();
        if (footerLayoutCount == 0) footerLayoutCount = 1;
        CoinItemDecoration coinItemDecoration = new CoinItemDecoration(getActivity(), DividerItemDecoration.VERTICAL, footerLayoutCount);
        mCoinRv.addItemDecoration(coinItemDecoration);

//        String userInfo = (String) SPUtils.get(mContext, "user_info", "");
//        mTvCoin.setText(userInfo.split("-")[3]);
        mTvCoin.setText(UserManager.getInstance().getUser().getCoinCount() + "");

        mSrl.setEnableRefresh(false);
        mSrl.setEnableLoadMore(false);

        mSrl.setOnLoadMoreListener(this);
        mSrl.setOnRefreshListener(this);

        int color = ToolsUtils.getThemeColor(getActivity());
        if (color != 0) {
            Log.i("zxd", "initView: " + color);
            mToolbar.setBackgroundColor(color);
            mSrlheader.setColorSchemeColors(color);
            mFooter.setAnimatingColor(color);
            mFooter.setNormalColor(color);
            mTvCoinText.setTextColor(color);
            mTvCoin.setTextColor(color);
        }
    }

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_coin;
    }

    @Override
    public void onSuccess(CommonResList<CoinSignInBean> data) {
        Log.i("zxd", "onSuccess: " + data.getDatas().size());
        hideLoading();
        if (data.getDatas().size() > 0) {
            mSrl.setEnableRefresh(true);
            mSrl.setEnableLoadMore(true);
        }
        if (data.isOver()) {
            mAdapter.loadMoreEnd();
            if (mAdapter.getFooterLayoutCount() == 0) {
                View footer = LayoutInflater.from(mContext).inflate(R.layout.loadmore_view, null);
                mAdapter.addFooterView(footer);
            }
            mSrl.setEnableLoadMore(false);
        }
        if (data.getDatas().size() == 0) return;

        mAdapter.addData(data.getDatas());
        page = data.getCurPage() + 1;
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        mCoinRv.postDelayed(() -> {
            mPresenter.getCoinSignInData(page);
            mSrl.finishLoadMore(true);
        }, 200);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (mAdapter.getFooterLayoutCount() > 0)
            mAdapter.removeAllFooterView();
        mCoinRv.postDelayed(() -> {
            mList.clear();
            mPresenter.getCoinSignInData(1);
            mSrl.finishRefresh(true);
        }, 200);
    }

    public static CoinFragment newInstance(String title) {
        CoinFragment fragment = new CoinFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onFailure(String msg) {
        ToastUtils.showToast(msg);
    }

    //没用
    @Override
    public void onRank(CommonResList<CoinBean> data) {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                dismiss();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (ToolsUtils.getNightMode(getActivity())) {
            getDialog().getWindow().setStatusBarColor(Color.TRANSPARENT);
            getDialog().getWindow().setNavigationBarColor(Color.TRANSPARENT);
        } else {
            int color = ToolsUtils.getThemeColor(getActivity());
            if (color != 0) {
                getDialog().getWindow().setStatusBarColor(color);
            }
            if (ToolsUtils.isOpenNavigationBar(getActivity())) {
                Window window = getDialog().getWindow();
                if (ToolsUtils.getThemeColor(getActivity()) != 0)
                    window.setNavigationBarColor(ToolsUtils.getThemeColor(getActivity()));
                else
                    window.setNavigationBarColor(ToolsUtils.getAttrValue(getActivity()));
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        page = 1;
    }
}
